﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Backend.DTO;
using Backend.BLL;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

namespace ReSeSSApplication
{
    public partial class ReviewStudy : System.Web.UI.Page
    {
        protected UserDTO loggedInUserDTO = new UserDTO();
        StudyBLL aStudyBLL = new StudyBLL();
        PatientBLL aPatientBLL = new PatientBLL();

        StudyDTO aStudy = null;
        List<PatientDTO> listOfPatientsDto = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                AlertError.Visible = false;
                AlertSuccess.Visible = false;

                loggedInUserDTO = (UserDTO)Session["LoggedInUserDTO"];
                RedirectUser(loggedInUserDTO);

                // checking required session for the page
                if (Session["StudyId"] == null)
                {
                    Response.Redirect("PreviousStudies.aspx");
                }
                else
                {
                    int studyId = Convert.ToInt32(Session["StudyId"]);
                    StudyIdHiddenField.Value = studyId.ToString();

                    aStudy = aStudyBLL.GetStudy(studyId);
                    listOfPatientsDto = aPatientBLL.GetPatientsByStudyId(studyId);

                    int totalPatients = listOfPatientsDto.Count;
                    int currentPatientIndex;

                    if (totalPatients > 0)
                    {
                        if (!IsPostBack)
                        {
                            if (totalPatients > 0)
                            {
                                currentPatientIndex = 1;
                            }
                            else
                            {
                                currentPatientIndex = 0;
                            }

                            CurrentPatientIndexHiddenField.Value = currentPatientIndex.ToString();
                        }
                    }

                    SetupPatient();
                }
            }
            catch(Exception ex)
            {
                ReviewTable.Visible = false;
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        List<string> listOfFieldName = new List<string>(); 

        /// <summary>
        /// Taken form the blog post
        /// http://blog.waleedmohamed.net/2009/03/export-generic-list-to-excel-file-in.html#.UCpCwKCsTpc
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="listOfPatientDto"></param>
        public void Export(string fileName, List<PatientDTO> listOfPatientDto)
        {
            // The Clear method erases any buffered HTML output.
            HttpContext.Current.Response.Clear();

            // The AddHeader method adds a new HTML header and value to the response sent to the client.
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName + ".xls"));

            // The ContentType property specifies the HTTP content type for the response.
            HttpContext.Current.Response.ContentType = "application/ms-excel";

            // Implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder.
            using (StringWriter sw = new StringWriter())
            {
                // Writes markup characters and text to an ASP.NET server control output stream. 
                // This class provides formatting capabilities that ASP.NET server controls use when rendering markup to clients.
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    //  Create a form to contain the List
                    Table table = new Table();
                    TableRow row = new TableRow();

                    for (int i = 0; i < listOfFieldName.Count; i++)
                    {
                        TableHeaderCell hcell = new TableHeaderCell();
                        hcell.Text = listOfFieldName[i];
                        row.Cells.Add(hcell);
                    }

                    table.Rows.Add(row);

                    //  add each of the data item to the table
                    int j = 1;
                    foreach (PatientDTO patient in listOfPatientDto)
                    {
                        TableRow row1 = new TableRow();

                        TableCell runningNumberCell = new TableCell();
                        runningNumberCell.Text = "" + j;
                        row1.Cells.Add(runningNumberCell);

                        TableCell nationalRegNumCell = new TableCell();
                        nationalRegNumCell.Text = "" + patient.Param_NationalRegistrationNumber;
                        row1.Cells.Add(nationalRegNumCell);

                        if (patient.IsReviewed)
                        {
                            TableCell ageCell = new TableCell();
                            ageCell.Text = "" + patient.Param_Age;
                            row1.Cells.Add(ageCell);

                            TableCell sexCell = new TableCell();
                            sexCell.Text = "" + patient.Param_Sex;
                            row1.Cells.Add(sexCell);

                            TableCell dateOfAddmissionCell = new TableCell();
                            dateOfAddmissionCell.Text = "" + patient.Param_DateOfAdmission;
                            row1.Cells.Add(dateOfAddmissionCell);

                            TableCell timeOfAddmissionCell = new TableCell();
                            timeOfAddmissionCell.Text = "" + patient.Param_TimeOfAdmission;
                            row1.Cells.Add(timeOfAddmissionCell);
                            
                            TableCell earlierEpisodesCell = new TableCell();
                            earlierEpisodesCell.Text = "" + patient.Param_EarlierEpisodesOfSbo;
                            row1.Cells.Add(earlierEpisodesCell);

                            TableCell abdominalSurgeryCell = new TableCell();
                            abdominalSurgeryCell.Text = "" + patient.Param_EarlierAbdominalSurgery;
                            row1.Cells.Add(abdominalSurgeryCell);

                            TableCell abdominalSurgery2Cell = new TableCell();
                            abdominalSurgery2Cell.Text = "" + patient.Param_EarlierAbdominalSurgery2;
                            row1.Cells.Add(abdominalSurgery2Cell);

                            TableCell abdominalSurgery3Cell = new TableCell();
                            abdominalSurgery3Cell.Text = "" + patient.Param_EarlierAbdominalSurgery3;
                            row1.Cells.Add(abdominalSurgery3Cell);

                            TableCell hypertensionCell = new TableCell();
                            hypertensionCell.Text = "" + patient.Param_Hypertension;
                            row1.Cells.Add(hypertensionCell);

                            TableCell anginaPectorisCell = new TableCell();
                            anginaPectorisCell.Text = "" + patient.Param_AnginaPectoris;
                            row1.Cells.Add(anginaPectorisCell);

                            TableCell heartFailureCell = new TableCell();
                            heartFailureCell.Text = "" + patient.Param_HeartFailure;
                            row1.Cells.Add(heartFailureCell);

                            TableCell amiCell = new TableCell();
                            amiCell.Text = "" + patient.Param_Ami;
                            row1.Cells.Add(amiCell);

                            TableCell strokeCell = new TableCell();
                            strokeCell.Text = "" + patient.Param_KidneyFailure;
                            row1.Cells.Add(strokeCell);

                            TableCell kidneyFailureCell = new TableCell();
                            kidneyFailureCell.Text = "" + patient.Param_KidneyFailure;
                            row1.Cells.Add(kidneyFailureCell);

                            TableCell diabetesCell = new TableCell();
                            diabetesCell.Text = "" + patient.Param_Diabetes;
                            row1.Cells.Add(diabetesCell);

                            TableCell pulmonaryDiseaseCell = new TableCell();
                            pulmonaryDiseaseCell.Text = "" + patient.Param_PulmonaryDisease;
                            row1.Cells.Add(pulmonaryDiseaseCell);

                            TableCell otherChronicDiseaseCell = new TableCell();
                            otherChronicDiseaseCell.Text = "" + patient.Param_OtherChronicDisease;
                            row1.Cells.Add(otherChronicDiseaseCell);

                            TableCell crpD0Cell = new TableCell();
                            crpD0Cell.Text = "" + patient.Param_CrpD0;
                            row1.Cells.Add(crpD0Cell);

                            TableCell crpMaxCell = new TableCell();
                            crpMaxCell.Text = "" + patient.Param_CrpMax;
                            row1.Cells.Add(crpMaxCell);

                            TableCell kreaD0Cell = new TableCell();
                            kreaD0Cell.Text = "" + patient.Param_KreaD0;
                            row1.Cells.Add(kreaD0Cell);

                            TableCell kreaMaxCell = new TableCell();
                            kreaMaxCell.Text = "" + patient.Param_KreaMax;
                            row1.Cells.Add(kreaMaxCell);

                            TableCell vomitCell = new TableCell();
                            vomitCell.Text = "" + patient.Param_VomitBeforeAdmission;
                            row1.Cells.Add(vomitCell);

                            TableCell vsondInitiallyCell = new TableCell();
                            vsondInitiallyCell.Text = "" + patient.Param_VsondInitially;
                            row1.Cells.Add(vsondInitiallyCell);

                            TableCell vsondLosesMaxCell = new TableCell();
                            vsondLosesMaxCell.Text = "" + patient.Param_VsondLossesMax;
                            row1.Cells.Add(vsondLosesMaxCell);

                            TableCell abdominalPainCell = new TableCell();
                            abdominalPainCell.Text = "" + patient.Param_AbdominalPain;
                            row1.Cells.Add(abdominalPainCell);

                            TableCell lengthOfHistoryCell = new TableCell();
                            lengthOfHistoryCell.Text = "" + patient.Param_LengthOfHistory;
                            row1.Cells.Add(lengthOfHistoryCell);

                            TableCell firstRadiologyCell = new TableCell();
                            firstRadiologyCell.Text = "" + patient.Param_1stRadiology;
                            row1.Cells.Add(firstRadiologyCell);

                            TableCell secondRadiologyCell = new TableCell();
                            secondRadiologyCell.Text = "" + patient.Param_2ndRadiology;
                            row1.Cells.Add(secondRadiologyCell);

                            TableCell thirdRadiologyCell = new TableCell();
                            thirdRadiologyCell.Text = "" + patient.Param_3rdRadiology;
                            row1.Cells.Add(thirdRadiologyCell);

                            TableCell forthRadiologyCell = new TableCell();
                            forthRadiologyCell.Text = "" + patient.Param_4thRadiology;
                            row1.Cells.Add(forthRadiologyCell);

                            TableCell fifthRadiologyCell = new TableCell();
                            fifthRadiologyCell.Text = "" + patient.Param_5thRadiology;
                            row1.Cells.Add(fifthRadiologyCell);

                            TableCell radiologyAnswerTimeCell = new TableCell();
                            radiologyAnswerTimeCell.Text = "" + patient.Param_TimeForRadiologyAnswer;
                            row1.Cells.Add(radiologyAnswerTimeCell);

                            TableCell radiologyAnswerCell = new TableCell();
                            radiologyAnswerCell.Text = "" + patient.Param_RadiologyAnswer;
                            row1.Cells.Add(radiologyAnswerCell);

                            TableCell affectedBowelsCell = new TableCell();
                            affectedBowelsCell.Text = "" + patient.Param_CirculatoryAffectedBowels;
                            row1.Cells.Add(affectedBowelsCell);

                            TableCell clinicalEvaluationCell = new TableCell();
                            clinicalEvaluationCell.Text = "" + patient.Param_ClinicalEvaluation;
                            row1.Cells.Add(clinicalEvaluationCell);

                            TableCell surgeryCell = new TableCell();
                            surgeryCell.Text = "" + patient.Param_Surgery;
                            row1.Cells.Add(surgeryCell);

                            TableCell surgicalAnnouncmentDateCell = new TableCell();
                            surgicalAnnouncmentDateCell.Text = "" + patient.Param_DateForSurgicalAnnouncment;
                            row1.Cells.Add(surgicalAnnouncmentDateCell);

                            TableCell dateSurgeryCell = new TableCell();
                            dateSurgeryCell.Text = "" + patient.Param_DateForSurgery;
                            row1.Cells.Add(dateSurgeryCell);

                            TableCell surgicalAnnouncementTimeCell = new TableCell();
                            surgicalAnnouncementTimeCell.Text = "" + patient.Param_TimeForSurgicalAnnouncement;
                            row1.Cells.Add(surgicalAnnouncementTimeCell);

                            TableCell timeUntilSurgeryCell = new TableCell();
                            timeUntilSurgeryCell.Text = "" + patient.Param_TimeUntilSurgery;
                            row1.Cells.Add(timeUntilSurgeryCell);

                            TableCell typeOfSurgeryCell = new TableCell();
                            typeOfSurgeryCell.Text = "" + patient.Param_TypeOfSurgery;
                            row1.Cells.Add(typeOfSurgeryCell);

                            TableCell secondSurgeryCell = new TableCell();
                            secondSurgeryCell.Text = "" + patient.Param_2ndSurgery;
                            row1.Cells.Add(secondSurgeryCell);

                            TableCell radiologicAnswerCorrectCell = new TableCell();
                            radiologicAnswerCorrectCell.Text = "" + patient.Param_WasRadiologicAnswerCorrect;
                            row1.Cells.Add(radiologicAnswerCorrectCell);

                            TableCell surgeryAccording2OrbitTimeCell = new TableCell();
                            surgeryAccording2OrbitTimeCell.Text = "" + patient.Param_TimeUntilSurgeryAccordingToOrbit;
                            row1.Cells.Add(surgeryAccording2OrbitTimeCell);

                            TableCell timeUnderSurgeryCell = new TableCell();
                            timeUnderSurgeryCell.Text = "" + patient.Param_TimeUnderSurgery;
                            row1.Cells.Add(timeUnderSurgeryCell);

                            TableCell diagnosisCell = new TableCell();
                            diagnosisCell.Text = "" + patient.Param_Diagnosis;
                            row1.Cells.Add(diagnosisCell);

                            TableCell diagnosisDischargeNoteCell = new TableCell();
                            diagnosisDischargeNoteCell.Text = "" + patient.Param_DiagnosisInDischargeNote;
                            row1.Cells.Add(diagnosisDischargeNoteCell);

                            TableCell malignantDiagnosisCell = new TableCell();
                            malignantDiagnosisCell.Text = "" + patient.Param_MalignantDiagnosis;
                            row1.Cells.Add(malignantDiagnosisCell);

                            TableCell bowelMovementTimeCell = new TableCell();
                            bowelMovementTimeCell.Text = "" + patient.Param_TimeUntilBowelMovement;
                            row1.Cells.Add(bowelMovementTimeCell);

                            TableCell postoperativeComplicationsCell = new TableCell();
                            postoperativeComplicationsCell.Text = "" + patient.Param_PostoperativeComplications;
                            row1.Cells.Add(postoperativeComplicationsCell);

                            TableCell reSurgeryCell = new TableCell();
                            reSurgeryCell.Text = "" + patient.Param_ReSurgery;
                            row1.Cells.Add(reSurgeryCell);

                            TableCell daysAdmittedCell = new TableCell();
                            daysAdmittedCell.Text = "" + patient.Param_DaysAdmitted;
                            row1.Cells.Add(daysAdmittedCell);

                            TableCell hoursAdmittedCell = new TableCell();
                            hoursAdmittedCell.Text = "" + patient.Param_HoursAdmitted;
                            row1.Cells.Add(hoursAdmittedCell);

                            TableCell daysICUCell = new TableCell();
                            daysICUCell.Text = "" + patient.Param_DaysAtIcu;
                            row1.Cells.Add(daysICUCell);

                            TableCell deadCell = new TableCell();
                            deadCell.Text = "" + patient.Param_Dead;
                            row1.Cells.Add(deadCell);

                            TableCell dateDeathCell = new TableCell();
                            dateDeathCell.Text = "" + patient.Param_DateOfDeath;
                            row1.Cells.Add(dateDeathCell);

                            TableCell deadWithin30DaysAdmissionCell = new TableCell();
                            deadWithin30DaysAdmissionCell.Text = "" + patient.Param_DeadWithin30DaysFromAdmission;
                            row1.Cells.Add(deadWithin30DaysAdmissionCell);

                            TableCell reAdmissionDue2SBOCell = new TableCell();
                            reAdmissionDue2SBOCell.Text = "" + patient.Param_ReAdmissionDueToSbo;
                            row1.Cells.Add(reAdmissionDue2SBOCell);

                            TableCell date4ReAdmissionCell = new TableCell();
                            date4ReAdmissionCell.Text = "" + patient.Param_DateForReAdmission;
                            row1.Cells.Add(date4ReAdmissionCell);

                            TableCell numberReAdmissionsCell = new TableCell();
                            numberReAdmissionsCell.Text = "" + patient.Param_NumberOfReAdmissions;
                            row1.Cells.Add(numberReAdmissionsCell);
                        }
                        else
                        {
                            for (int k = 0; k < listOfFieldName.Count - 3; k++)
                            {
                                TableCell emptyCell = new TableCell();
                                emptyCell.Text = "-1";
                                row1.Cells.Add(emptyCell);
                            }
                        }

                        TableCell statusCell = new TableCell();

                        if (patient.IsFlagged)
                        {
                            statusCell.Text = "2";
                        }
                        else if (patient.IsReviewed)
                        {
                            statusCell.Text = "1";
                        }
                        else
                        {
                            statusCell.Text = "0";
                        }

                        row1.Cells.Add(statusCell);

                        table.Rows.Add(row1);
                        j++;
                    }

                    //  render the table into the htmlwriter
                    table.RenderControl(htw);

                    //  render the htmlwriter into the response
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();
                }
            }
        }

        protected void ExportPatientButton_Click(object sender, EventArgs e)
        {
            try
            {
                listOfFieldName.Add("Running No.");
                listOfFieldName.Add("National Registration Number");
                listOfFieldName.Add("Age");
                listOfFieldName.Add("Sex");
                listOfFieldName.Add("Date of Admission");
                listOfFieldName.Add("Time of Admission");
                listOfFieldName.Add("Earlier Episodes of SBO");
                listOfFieldName.Add("Earlier Abdominal Surgery");
                listOfFieldName.Add("Earlier Abdominal Surgery 2");
                listOfFieldName.Add("Earlier Abdominal Surgery 3");
                
                listOfFieldName.Add("Hypertension");
                listOfFieldName.Add("Angina Pectoris");
                listOfFieldName.Add("Heart Failure");
                listOfFieldName.Add("AMI");
                listOfFieldName.Add("Stroke / TIA");
                listOfFieldName.Add("Kidney Failure");
                listOfFieldName.Add("Diabetes");
                listOfFieldName.Add("Pulmonary Disease");
                listOfFieldName.Add("Other Chronic Disease");
                listOfFieldName.Add("CRP D0");
                
                listOfFieldName.Add("CRP Max");
                listOfFieldName.Add("Krea D0");
                listOfFieldName.Add("Krea Max");
                listOfFieldName.Add("Vomit Before Admission");
                listOfFieldName.Add("V-sond Initially");
                listOfFieldName.Add("V-sond Losses Max");
                listOfFieldName.Add("Abdominal Pain");
                listOfFieldName.Add("Length of History");
                listOfFieldName.Add("1st Radiology");
                listOfFieldName.Add("2nd Radiology");

                listOfFieldName.Add("3rd Radiology");
                listOfFieldName.Add("4th Radiology");
                listOfFieldName.Add("5th Radiology");
                listOfFieldName.Add("Time for Radiology Answer");
                listOfFieldName.Add("Radiology Answer");
                listOfFieldName.Add("Circulatory Affected Bowels");
                listOfFieldName.Add("Clinical Evaluation");
                listOfFieldName.Add("Surgery");
                listOfFieldName.Add("Date for Surgical Announcment");
                listOfFieldName.Add("Date for Surgery");

                listOfFieldName.Add("Time for Surgical Announcement");
                listOfFieldName.Add("Time Until Surgery");
                listOfFieldName.Add("Type of Surgery");
                listOfFieldName.Add("2nd Surgery");
                listOfFieldName.Add("Was Radiologic Answer Correct?");
                listOfFieldName.Add("Time Until Surgery According to Orbit");
                listOfFieldName.Add("Time Under Surgery");
                listOfFieldName.Add("Diagnosis");
                listOfFieldName.Add("Diagnosis in Discharge Note");
                listOfFieldName.Add("Malignant Diagnosis");

                listOfFieldName.Add("Time Until Bowel Movement");
                listOfFieldName.Add("Postoperative Complications");
                listOfFieldName.Add("Re-surgery");
                listOfFieldName.Add("Days Admitted");
                listOfFieldName.Add("Hours Admitted");
                listOfFieldName.Add("Days at ICU");
                listOfFieldName.Add("Dead");
                listOfFieldName.Add("Date of Death");
                listOfFieldName.Add("Dead within 30 Days from Admission");
                listOfFieldName.Add("Re-admission due to SBO");

                listOfFieldName.Add("Date for Re-admission");
                listOfFieldName.Add("Number of Re-admissions");
                listOfFieldName.Add("Reviewed");

                string cleanStudyName = Regex.Replace(aStudy.Name, @"[^a-zA-Z0-9]+", "_");
                Export(cleanStudyName + "_" + DateTime.Now.ToString("yyyy-MM-dd") + "", listOfPatientsDto);
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void BackButton_Click(object sender, EventArgs e)
        {
            try
            {
                Session["StudyId"] = null;
                Response.Redirect("PreviousStudies.aspx");
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void PrevLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                int currentPatientIndex = Convert.ToInt32(CurrentPatientIndexHiddenField.Value);

                if (currentPatientIndex > 1)
                {
                    CurrentPatientIndexHiddenField.Value = (currentPatientIndex - 1).ToString();
                }
                else
                {
                    PrevLinkButton.Enabled = false;
                    PrevLinkButton.CssClass = "btn btn-large disabled";
                    CurrentPatientIndexHiddenField.Value = "1";
                }

                SetupPatient();
            }
            catch(Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void NextLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                int totalPatients = listOfPatientsDto.Count;
                int currentPatientIndex = Convert.ToInt32(CurrentPatientIndexHiddenField.Value);

                if (currentPatientIndex < totalPatients)
                {
                    CurrentPatientIndexHiddenField.Value = (currentPatientIndex + 1).ToString();
                }
                else
                {
                    PrevLinkButton.Enabled = false;
                    PrevLinkButton.CssClass = "btn btn-large disabled";
                    CurrentPatientIndexHiddenField.Value = totalPatients.ToString();
                }

                SetupPatient();
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void FlagLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                int studyId = Convert.ToInt32(StudyIdHiddenField.Value);
                int patientId = Convert.ToInt32(CurrentPatientIdHiddenField.Value);

                if (FlagLinkButton.CssClass.Contains("active"))
                {
                    /* patient is flagged. make it unflagged. */

                    FlagLinkButton.CssClass = "btn btn-large";
                    FlagLinkButton.ToolTip = "Mark as Flag";

                    if (aPatientBLL.UpdateFlagProperty(studyId, patientId, false))
                    {
                        AlertSuccess.InnerText = "Patient successfully mark as unflagged.";
                        AlertSuccess.Visible = true;
                    }
                    else
                    {
                        AlertError.InnerText = "Patient failed to mark as unflagged.";
                        AlertError.Visible = true;
                    }
                }
                else
                {
                    /* patient is unflagged. make it flagged. */

                    FlagLinkButton.CssClass = "btn btn-large active";
                    FlagLinkButton.ToolTip = "Mark as Unflag";

                    if (aPatientBLL.UpdateFlagProperty(studyId, patientId, true))
                    {
                        AlertSuccess.InnerText = "Patient successfully mark as flagged.";
                        AlertSuccess.Visible = true;
                    }
                    else
                    {
                        AlertError.InnerText = "Patient failed to mark as flagged.";
                        AlertError.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void ReviewLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                int studyId = Convert.ToInt32(StudyIdHiddenField.Value);
                int patientId = Convert.ToInt32(CurrentPatientIdHiddenField.Value);

                if (ReviewLinkButton.CssClass.Contains("active"))
                {
                    /* patient is reviewed. make it unreviewed. */

                    ReviewLinkButton.CssClass = "btn btn-large";
                    ReviewLinkButton.ToolTip = "Mark as Reviewed";

                    if (aPatientBLL.UpdateReviewProperty(studyId, patientId, false))
                    {
                        AlertSuccess.InnerText = "Patient successfully mark as unreviewed.";
                        AlertSuccess.Visible = true;
                    }
                    else
                    {
                        AlertError.InnerText = "Patient failed to mark as unreviewed.";
                        AlertError.Visible = true;
                    }
                }
                else
                {
                    /* patient is unreviewed. make it reviewed. */

                    ReviewLinkButton.CssClass = "btn btn-large active";
                    ReviewLinkButton.ToolTip = "Unmark as Reviewed";

                    if (aPatientBLL.UpdateReviewProperty(studyId, patientId, true))
                    {
                        AlertSuccess.InnerText = "Patient successfully mark as reviewed.";
                        AlertSuccess.Visible = true;
                    }
                    else
                    {
                        AlertError.InnerText = "Patient failed to mark as reviewed.";
                        AlertError.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        private void SetupPatient()
        {
            try
            {
                int totalPatients = listOfPatientsDto.Count;
                int currentPatientIndex = Convert.ToInt32(CurrentPatientIndexHiddenField.Value);

                if (totalPatients > 0)
                {
                    PatientDTO currentPatient = listOfPatientsDto[currentPatientIndex - 1];

                    // setup heading
                    lblStudyName.Text = aStudy.Name;
                    lblCurrentPatientNumber.Text = currentPatientIndex.ToString();
                    lblTotalPatientNumber.Text = totalPatients.ToString();

                    SetupControls(totalPatients, currentPatientIndex, currentPatient);
                    SetPatientParameter(currentPatient);
                }
                else
                {
                    ReviewTable.Visible = false;
                    PatientControlsDiv.Visible = false;

                    AlertError.InnerText = "No patient associated with this study. Try to extract study first.";
                    AlertError.Visible = true;
                }
            }
            catch (Exception ex)
            {
                ReviewTable.Visible = false;
                PatientControlsDiv.Visible = false;

                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        private void SetPatientParameter(PatientDTO currentPatient)
        {
            CurrentPatientIdHiddenField.Value = currentPatient.Id.ToString();


            // -- Section A
            lblNationalRegistrationNumber.Text = currentPatient.Param_NationalRegistrationNumber;
            lblAge.Text = currentPatient.Param_Age.ToString();
            lblSex.Text = currentPatient.Param_Sex;
            lblDateOfAdmission.Text = currentPatient.Param_DateOfAdmission.ToString("yyyy-MM-dd");
            lblTimeOfAdmission.Text = currentPatient.Param_TimeOfAdmission.ToString("HH:mm tt");

            string earlierEpisodesOfSboText = string.Empty;
            earlierEpisodesOfSboText = currentPatient.Param_EarlierEpisodesOfSbo ? "YES" : "NO";
            lblEarlierEpisodesOfSbo.Text = earlierEpisodesOfSboText;

            lblEarlierAbdominalSurgery.Text = currentPatient.Param_EarlierAbdominalSurgery.ToString();
            lblEarlierAbdominalSurgery2.Text = currentPatient.Param_EarlierAbdominalSurgery2.ToString();
            lblEarlierAbdominalSurgery3.Text = currentPatient.Param_EarlierAbdominalSurgery3.ToString();

            string hypertensionText = string.Empty;
            hypertensionText = currentPatient.Param_Hypertension ? "YES" : "NO";
            lblHypertension.Text = hypertensionText;

            string anginaPectorisText = string.Empty;
            anginaPectorisText = currentPatient.Param_AnginaPectoris ? "YES" : "NO";
            lblAnginaPectoris.Text = anginaPectorisText;

            string heartFailureText = string.Empty;
            heartFailureText = currentPatient.Param_HeartFailure ? "YES" : "NO";
            lblHeartFailure.Text = earlierEpisodesOfSboText;

            string amiText = string.Empty;
            amiText = currentPatient.Param_Ami ? "YES" : "NO";
            lblAmi.Text = earlierEpisodesOfSboText;

            string strokeTiaText = string.Empty;
            strokeTiaText = currentPatient.Param_StrokeTia ? "YES" : "NO";
            lblStrokeTia.Text = strokeTiaText;

            string kidneyFailureText = string.Empty;
            kidneyFailureText = currentPatient.Param_KidneyFailure ? "YES" : "NO";
            lblKidneyFailure.Text = kidneyFailureText;

            string diabetesText = string.Empty;
            diabetesText = currentPatient.Param_Diabetes ? "YES" : "NO";
            lblDiabetes.Text = diabetesText;

            lblPulmonaryDisease.Text = currentPatient.Param_PulmonaryDisease.ToString();

            string otherChronicDiseaseText = string.Empty;
            otherChronicDiseaseText = currentPatient.Param_OtherChronicDisease ? "YES" : "NO";
            lblOtherChronicDisease.Text = otherChronicDiseaseText;

            lblTimeForRadiologyAnswer.Text = currentPatient.Param_TimeForRadiologyAnswer.ToString("yyyy-MM-dd");

            string surgeryText = string.Empty;
            surgeryText = currentPatient.Param_Surgery ? "YES" : "NO";
            lblSurgery.Text = surgeryText;

            lblDateForSurgicalAnnouncment.Text = currentPatient.Param_DateForSurgicalAnnouncment.ToString("yyyy-MM-dd");
            lblDateForSurgery.Text = currentPatient.Param_DateForSurgery.ToString("yyyy-MM-dd");
            lblTimeForSurgicalAnnouncement.Text = currentPatient.Param_TimeForSurgicalAnnouncement.ToString("yyyy-MM-dd");
            lblTimeUntilSurgery.Text = currentPatient.Param_TimeUntilSurgery.ToString("yyyy-MM-dd");
            lblTimeUntilSurgeryAccordingToOrbit.Text = currentPatient.Param_TimeUntilSurgeryAccordingToOrbit.ToString("yyyy-MM-dd");
            lblDiagnosis.Text = currentPatient.Param_Diagnosis;
            lblDiagnosisInDischargeNote.Text = currentPatient.Param_DiagnosisInDischargeNote.ToString();
            lblMalignantDiagnosis.Text = currentPatient.Param_MalignantDiagnosis.ToString();
            lblTimeUntilBowelMovement.Text = currentPatient.Param_TimeUntilBowelMovement.ToString();

            string reSurgeryText = string.Empty;
            reSurgeryText = currentPatient.Param_ReSurgery ? "YES" : "NO";
            lblReSurgery.Text = reSurgeryText;

            lblDaysAdmitted.Text = currentPatient.Param_DaysAdmitted.ToString("yyyy-MM-dd");
            lblHoursAdmitted.Text = currentPatient.Param_HoursAdmitted.ToString("HH:mm tt");

            string deadText = string.Empty;
            deadText = currentPatient.Param_Dead ? "YES" : "NO";
            lblDead.Text = deadText;

            lblDateOfDeath.Text = currentPatient.Param_DateOfDeath.ToString("yyyy-MM-dd");

            string deadWithin30DaysFromAdmissionText = string.Empty;
            deadWithin30DaysFromAdmissionText = currentPatient.Param_DeadWithin30DaysFromAdmission ? "YES" : "NO";
            lblDeadWithin30DaysFromAdmission.Text = deadWithin30DaysFromAdmissionText;

            string reAdmissionDueToSboText = string.Empty;
            reAdmissionDueToSboText = currentPatient.Param_ReAdmissionDueToSbo ? "YES" : "NO";
            lblReAdmissionDueToSbo.Text = reAdmissionDueToSboText;

            lblDateForReAdmission.Text = currentPatient.Param_DateForReAdmission.ToString("yyyy-MM-dd");


            // -- Section B
            lblCrpD0.Text = currentPatient.Param_CrpD0.ToString();
            lblCrpMax.Text = currentPatient.Param_CrpMax.ToString();
            lblKreaD0.Text = currentPatient.Param_KreaD0.ToString();
            lblKreaMax.Text = currentPatient.Param_KreaMax.ToString();

            string abdominalPainText = string.Empty;
            abdominalPainText = currentPatient.Param_AbdominalPain ? "YES" : "NO";
            lblAbdominalPain.Text = abdominalPainText;


            // -- Section C
            if (currentPatient.Param_VomitBeforeAdmission)
            {
                VomitBeforeAdmissionNoRadioButton.Checked = false;
                VomitBeforeAdmissionYesRadioButton.Checked = true;
            }
            else
            {
                VomitBeforeAdmissionNoRadioButton.Checked = true;
                VomitBeforeAdmissionYesRadioButton.Checked = false;
            }

            if (currentPatient.Param_VsondInitially)
            {
                VsondInitiallyNoRadioButton.Checked = false;
                VsondInitiallyYesRadioButton.Checked = true;
            }
            else
            {
                VsondInitiallyNoRadioButton.Checked = true;
                VsondInitiallyYesRadioButton.Checked = false;
            }

            // Param_VsondLossesMax
            if (currentPatient.Param_VsondLossesMax == 0)
            {
                VsondLossesMaxRadioButton.Checked = true;
            }
            else
            {
                VsondLossesMaxRadioButton.Checked = false;
            }

            txtLengthOfHistory.Text = currentPatient.Param_LengthOfHistory.ToString();

            // Param_ClinicalEvaluation
            if (currentPatient.Param_ClinicalEvaluation == 0)
            {
                rbtnClinicalEvaluation0.Checked = true;
                rbtnClinicalEvaluation1.Checked = false;
                rbtnClinicalEvaluation2.Checked = false;
                rbtnClinicalEvaluation3.Checked = false;
                rbtnClinicalEvaluation4.Checked = false;
            }
            else if (currentPatient.Param_ClinicalEvaluation == 1)
            {
                rbtnClinicalEvaluation0.Checked = false;
                rbtnClinicalEvaluation1.Checked = true;
                rbtnClinicalEvaluation2.Checked = false;
                rbtnClinicalEvaluation3.Checked = false;
                rbtnClinicalEvaluation4.Checked = false;
            }
            else if (currentPatient.Param_ClinicalEvaluation == 2)
            {
                rbtnClinicalEvaluation0.Checked = false;
                rbtnClinicalEvaluation1.Checked = false;
                rbtnClinicalEvaluation2.Checked = true;
                rbtnClinicalEvaluation3.Checked = false;
                rbtnClinicalEvaluation4.Checked = false;
            }
            else if (currentPatient.Param_ClinicalEvaluation == 3)
            {
                rbtnClinicalEvaluation0.Checked = false;
                rbtnClinicalEvaluation1.Checked = false;
                rbtnClinicalEvaluation2.Checked = false;
                rbtnClinicalEvaluation3.Checked = true;
                rbtnClinicalEvaluation4.Checked = false;
            }
            else if (currentPatient.Param_ClinicalEvaluation == 4)
            {
                rbtnClinicalEvaluation0.Checked = false;
                rbtnClinicalEvaluation1.Checked = false;
                rbtnClinicalEvaluation2.Checked = false;
                rbtnClinicalEvaluation3.Checked = false;
                rbtnClinicalEvaluation4.Checked = true;
            }

            // Param_TypeOfSurgery
            if (currentPatient.Param_TypeOfSurgery == 0)
            {
                rbtnTypeOfSurgery0.Checked = true;
                rbtnTypeOfSurgery1.Checked = false;
                rbtnTypeOfSurgery2.Checked = false;
                rbtnTypeOfSurgery3.Checked = false;
                rbtnTypeOfSurgery4.Checked = false;
                rbtnTypeOfSurgery5.Checked = false;
                rbtnTypeOfSurgery6.Checked = false;
                rbtnTypeOfSurgery7.Checked = false;
                rbtnTypeOfSurgery8.Checked = false;
            }
            else if (currentPatient.Param_TypeOfSurgery == 1)
            {
                rbtnTypeOfSurgery0.Checked = false;
                rbtnTypeOfSurgery1.Checked = true;
                rbtnTypeOfSurgery2.Checked = false;
                rbtnTypeOfSurgery3.Checked = false;
                rbtnTypeOfSurgery4.Checked = false;
                rbtnTypeOfSurgery5.Checked = false;
                rbtnTypeOfSurgery6.Checked = false;
                rbtnTypeOfSurgery7.Checked = false;
                rbtnTypeOfSurgery8.Checked = false;
            }
            else if (currentPatient.Param_TypeOfSurgery == 2)
            {
                rbtnTypeOfSurgery0.Checked = false;
                rbtnTypeOfSurgery1.Checked = false;
                rbtnTypeOfSurgery2.Checked = true;
                rbtnTypeOfSurgery3.Checked = false;
                rbtnTypeOfSurgery4.Checked = false;
                rbtnTypeOfSurgery5.Checked = false;
                rbtnTypeOfSurgery6.Checked = false;
                rbtnTypeOfSurgery7.Checked = false;
                rbtnTypeOfSurgery8.Checked = false;
            }
            else if (currentPatient.Param_TypeOfSurgery == 3)
            {
                rbtnTypeOfSurgery0.Checked = false;
                rbtnTypeOfSurgery1.Checked = false;
                rbtnTypeOfSurgery2.Checked = false;
                rbtnTypeOfSurgery3.Checked = true;
                rbtnTypeOfSurgery4.Checked = false;
                rbtnTypeOfSurgery5.Checked = false;
                rbtnTypeOfSurgery6.Checked = false;
                rbtnTypeOfSurgery7.Checked = false;
                rbtnTypeOfSurgery8.Checked = false;
            }
            else if (currentPatient.Param_TypeOfSurgery == 4)
            {
                rbtnTypeOfSurgery0.Checked = false;
                rbtnTypeOfSurgery1.Checked = false;
                rbtnTypeOfSurgery2.Checked = false;
                rbtnTypeOfSurgery3.Checked = false;
                rbtnTypeOfSurgery4.Checked = true;
                rbtnTypeOfSurgery5.Checked = false;
                rbtnTypeOfSurgery6.Checked = false;
                rbtnTypeOfSurgery7.Checked = false;
                rbtnTypeOfSurgery8.Checked = false;
            }
            else if (currentPatient.Param_TypeOfSurgery == 5)
            {
                rbtnTypeOfSurgery0.Checked = false;
                rbtnTypeOfSurgery1.Checked = false;
                rbtnTypeOfSurgery2.Checked = false;
                rbtnTypeOfSurgery3.Checked = false;
                rbtnTypeOfSurgery4.Checked = false;
                rbtnTypeOfSurgery5.Checked = true;
                rbtnTypeOfSurgery6.Checked = false;
                rbtnTypeOfSurgery7.Checked = false;
                rbtnTypeOfSurgery8.Checked = false;
            }
            else if (currentPatient.Param_TypeOfSurgery == 6)
            {
                rbtnTypeOfSurgery0.Checked = false;
                rbtnTypeOfSurgery1.Checked = false;
                rbtnTypeOfSurgery2.Checked = false;
                rbtnTypeOfSurgery3.Checked = false;
                rbtnTypeOfSurgery4.Checked = false;
                rbtnTypeOfSurgery5.Checked = false;
                rbtnTypeOfSurgery6.Checked = true;
                rbtnTypeOfSurgery7.Checked = false;
                rbtnTypeOfSurgery8.Checked = false;
            }
            else if (currentPatient.Param_TypeOfSurgery == 7)
            {
                rbtnTypeOfSurgery0.Checked = false;
                rbtnTypeOfSurgery1.Checked = false;
                rbtnTypeOfSurgery2.Checked = false;
                rbtnTypeOfSurgery3.Checked = false;
                rbtnTypeOfSurgery4.Checked = false;
                rbtnTypeOfSurgery5.Checked = false;
                rbtnTypeOfSurgery6.Checked = false;
                rbtnTypeOfSurgery7.Checked = true;
                rbtnTypeOfSurgery8.Checked = false;
            }
            else if (currentPatient.Param_TypeOfSurgery == 8)
            {
                rbtnTypeOfSurgery0.Checked = false;
                rbtnTypeOfSurgery1.Checked = false;
                rbtnTypeOfSurgery2.Checked = false;
                rbtnTypeOfSurgery3.Checked = false;
                rbtnTypeOfSurgery4.Checked = false;
                rbtnTypeOfSurgery5.Checked = false;
                rbtnTypeOfSurgery6.Checked = false;
                rbtnTypeOfSurgery7.Checked = false;
                rbtnTypeOfSurgery8.Checked = true;
            }

            // Param_2ndSurgery
            if (currentPatient.Param_2ndSurgery == 0)
            {
                rbtn2ndSurgery0.Checked = true;
                rbtn2ndSurgery1.Checked = false;
                rbtn2ndSurgery2.Checked = false;
                rbtn2ndSurgery3.Checked = false;
                rbtn2ndSurgery4.Checked = false;
                rbtn2ndSurgery5.Checked = false;
                rbtn2ndSurgery6.Checked = false;
                rbtn2ndSurgery7.Checked = false;
                rbtn2ndSurgery8.Checked = false;
            }
            else if (currentPatient.Param_2ndSurgery == 1)
            {
                rbtn2ndSurgery0.Checked = false;
                rbtn2ndSurgery1.Checked = true;
                rbtn2ndSurgery2.Checked = false;
                rbtn2ndSurgery3.Checked = false;
                rbtn2ndSurgery4.Checked = false;
                rbtn2ndSurgery5.Checked = false;
                rbtn2ndSurgery6.Checked = false;
                rbtn2ndSurgery7.Checked = false;
                rbtn2ndSurgery8.Checked = false;
            }
            else if (currentPatient.Param_2ndSurgery == 2)
            {
                rbtn2ndSurgery0.Checked = false;
                rbtn2ndSurgery1.Checked = false;
                rbtn2ndSurgery2.Checked = true;
                rbtn2ndSurgery3.Checked = false;
                rbtn2ndSurgery4.Checked = false;
                rbtn2ndSurgery5.Checked = false;
                rbtn2ndSurgery6.Checked = false;
                rbtn2ndSurgery7.Checked = false;
                rbtn2ndSurgery8.Checked = false;
            }
            else if (currentPatient.Param_2ndSurgery == 3)
            {
                rbtn2ndSurgery0.Checked = false;
                rbtn2ndSurgery1.Checked = false;
                rbtn2ndSurgery2.Checked = false;
                rbtn2ndSurgery3.Checked = true;
                rbtn2ndSurgery4.Checked = false;
                rbtn2ndSurgery5.Checked = false;
                rbtn2ndSurgery6.Checked = false;
                rbtn2ndSurgery7.Checked = false;
                rbtn2ndSurgery8.Checked = false;
            }
            else if (currentPatient.Param_2ndSurgery == 4)
            {
                rbtn2ndSurgery0.Checked = false;
                rbtn2ndSurgery1.Checked = false;
                rbtn2ndSurgery2.Checked = false;
                rbtn2ndSurgery3.Checked = false;
                rbtn2ndSurgery4.Checked = true;
                rbtn2ndSurgery5.Checked = false;
                rbtn2ndSurgery6.Checked = false;
                rbtn2ndSurgery7.Checked = false;
                rbtn2ndSurgery8.Checked = false;
            }
            else if (currentPatient.Param_2ndSurgery == 5)
            {
                rbtn2ndSurgery0.Checked = false;
                rbtn2ndSurgery1.Checked = false;
                rbtn2ndSurgery2.Checked = false;
                rbtn2ndSurgery3.Checked = false;
                rbtn2ndSurgery4.Checked = false;
                rbtn2ndSurgery5.Checked = true;
                rbtn2ndSurgery6.Checked = false;
                rbtn2ndSurgery7.Checked = false;
                rbtn2ndSurgery8.Checked = false;
            }
            else if (currentPatient.Param_2ndSurgery == 6)
            {
                rbtn2ndSurgery0.Checked = false;
                rbtn2ndSurgery1.Checked = false;
                rbtn2ndSurgery2.Checked = false;
                rbtn2ndSurgery3.Checked = false;
                rbtn2ndSurgery4.Checked = false;
                rbtn2ndSurgery5.Checked = false;
                rbtn2ndSurgery6.Checked = true;
                rbtn2ndSurgery7.Checked = false;
                rbtn2ndSurgery8.Checked = false;
            }
            else if (currentPatient.Param_2ndSurgery == 7)
            {
                rbtn2ndSurgery0.Checked = false;
                rbtn2ndSurgery1.Checked = false;
                rbtn2ndSurgery2.Checked = false;
                rbtn2ndSurgery3.Checked = false;
                rbtn2ndSurgery4.Checked = false;
                rbtn2ndSurgery5.Checked = false;
                rbtn2ndSurgery6.Checked = false;
                rbtn2ndSurgery7.Checked = true;
                rbtn2ndSurgery8.Checked = false;
            }
            else if (currentPatient.Param_2ndSurgery == 8)
            {
                rbtn2ndSurgery0.Checked = false;
                rbtn2ndSurgery1.Checked = false;
                rbtn2ndSurgery2.Checked = false;
                rbtn2ndSurgery3.Checked = false;
                rbtn2ndSurgery4.Checked = false;
                rbtn2ndSurgery5.Checked = false;
                rbtn2ndSurgery6.Checked = false;
                rbtn2ndSurgery7.Checked = false;
                rbtn2ndSurgery8.Checked = true;
            }

            // Param_WasRadiologicAnswerCorrect
            if (currentPatient.Param_WasRadiologicAnswerCorrect == 0)
            {
                rbtnWasRadiologicAnswerCorrect0.Checked = true;
                rbtnWasRadiologicAnswerCorrect1.Checked = false;
                rbtnWasRadiologicAnswerCorrect2.Checked = false;
            }
            else if (currentPatient.Param_WasRadiologicAnswerCorrect == 1)
            {
                rbtnWasRadiologicAnswerCorrect0.Checked = false;
                rbtnWasRadiologicAnswerCorrect1.Checked = true;
                rbtnWasRadiologicAnswerCorrect2.Checked = false;
            }
            else if (currentPatient.Param_WasRadiologicAnswerCorrect == 2)
            {
                rbtnWasRadiologicAnswerCorrect0.Checked = false;
                rbtnWasRadiologicAnswerCorrect1.Checked = false;
                rbtnWasRadiologicAnswerCorrect2.Checked = true;
            }

            txtTimeUnderSurgery.Text = currentPatient.Param_TimeUnderSurgery.ToString();

            // Param_PostoperativeComplications
            if (currentPatient.Param_PostoperativeComplications == 0)
            {
                rbtnPostoperativeComplications0.Checked = true;
                rbtnPostoperativeComplications1.Checked = false;
                rbtnPostoperativeComplications2.Checked = false;
                rbtnPostoperativeComplications3.Checked = false;
                rbtnPostoperativeComplications4.Checked = false;
                rbtnPostoperativeComplications5.Checked = false;
            }
            else if (currentPatient.Param_PostoperativeComplications == 1)
            {
                rbtnPostoperativeComplications0.Checked = false;
                rbtnPostoperativeComplications1.Checked = true;
                rbtnPostoperativeComplications2.Checked = false;
                rbtnPostoperativeComplications3.Checked = false;
                rbtnPostoperativeComplications4.Checked = false;
                rbtnPostoperativeComplications5.Checked = false;
            }
            else if (currentPatient.Param_PostoperativeComplications == 2)
            {
                rbtnPostoperativeComplications0.Checked = false;
                rbtnPostoperativeComplications1.Checked = false;
                rbtnPostoperativeComplications2.Checked = true;
                rbtnPostoperativeComplications3.Checked = false;
                rbtnPostoperativeComplications4.Checked = false;
                rbtnPostoperativeComplications5.Checked = false;
            }
            else if (currentPatient.Param_PostoperativeComplications == 3)
            {
                rbtnPostoperativeComplications0.Checked = false;
                rbtnPostoperativeComplications1.Checked = false;
                rbtnPostoperativeComplications2.Checked = false;
                rbtnPostoperativeComplications3.Checked = true;
                rbtnPostoperativeComplications4.Checked = false;
                rbtnPostoperativeComplications5.Checked = false;
            }
            else if (currentPatient.Param_PostoperativeComplications == 4)
            {
                rbtnPostoperativeComplications0.Checked = false;
                rbtnPostoperativeComplications1.Checked = false;
                rbtnPostoperativeComplications2.Checked = false;
                rbtnPostoperativeComplications3.Checked = false;
                rbtnPostoperativeComplications4.Checked = true;
                rbtnPostoperativeComplications5.Checked = false;
            }
            else if (currentPatient.Param_PostoperativeComplications == 5)
            {
                rbtnPostoperativeComplications0.Checked = false;
                rbtnPostoperativeComplications1.Checked = false;
                rbtnPostoperativeComplications2.Checked = false;
                rbtnPostoperativeComplications3.Checked = false;
                rbtnPostoperativeComplications4.Checked = false;
                rbtnPostoperativeComplications5.Checked = true;
            }

            txtDaysAtIcu.Text = currentPatient.Param_DaysAtIcu.ToString();


            // -- Section D

            // Param_1stRadiology
            if (currentPatient.Param_1stRadiology == 0)
            {
                rbtn1stRadiology0.Checked = true;
                rbtn1stRadiology1.Checked = false;
                rbtn1stRadiology2.Checked = false;
                rbtn1stRadiology3.Checked = false;
                rbtn1stRadiology4.Checked = false;
            }
            else if (currentPatient.Param_1stRadiology == 1)
            {
                rbtn1stRadiology0.Checked = false;
                rbtn1stRadiology1.Checked = true;
                rbtn1stRadiology2.Checked = false;
                rbtn1stRadiology3.Checked = false;
                rbtn1stRadiology4.Checked = false;
            }
            else if (currentPatient.Param_1stRadiology == 2)
            {
                rbtn1stRadiology0.Checked = false;
                rbtn1stRadiology1.Checked = false;
                rbtn1stRadiology2.Checked = true;
                rbtn1stRadiology3.Checked = false;
                rbtn1stRadiology4.Checked = false;
            }
            else if (currentPatient.Param_1stRadiology == 3)
            {
                rbtn1stRadiology0.Checked = false;
                rbtn1stRadiology1.Checked = false;
                rbtn1stRadiology2.Checked = false;
                rbtn1stRadiology3.Checked = true;
                rbtn1stRadiology4.Checked = false;
            }
            else if (currentPatient.Param_1stRadiology == 4)
            {
                rbtn1stRadiology0.Checked = false;
                rbtn1stRadiology1.Checked = false;
                rbtn1stRadiology2.Checked = false;
                rbtn1stRadiology3.Checked = false;
                rbtn1stRadiology4.Checked = true;
            }


            // Param_2ndRadiology
            if (currentPatient.Param_2ndRadiology == 0)
            {
                rbtn2ndRadiology0.Checked = true;
                rbtn2ndRadiology1.Checked = false;
                rbtn2ndRadiology2.Checked = false;
                rbtn2ndRadiology3.Checked = false;
                rbtn2ndRadiology4.Checked = false;
                rbtn2ndRadiology5.Checked = false;
            }
            else if (currentPatient.Param_2ndRadiology == 1)
            {
                rbtn2ndRadiology0.Checked = false;
                rbtn2ndRadiology1.Checked = true;
                rbtn2ndRadiology2.Checked = false;
                rbtn2ndRadiology3.Checked = false;
                rbtn2ndRadiology4.Checked = false;
                rbtn2ndRadiology5.Checked = false;
            }
            else if (currentPatient.Param_2ndRadiology == 2)
            {
                rbtn2ndRadiology0.Checked = false;
                rbtn2ndRadiology1.Checked = false;
                rbtn2ndRadiology2.Checked = true;
                rbtn2ndRadiology3.Checked = false;
                rbtn2ndRadiology4.Checked = false;
                rbtn2ndRadiology5.Checked = false;
            }
            else if (currentPatient.Param_2ndRadiology == 3)
            {
                rbtn2ndRadiology0.Checked = false;
                rbtn2ndRadiology1.Checked = false;
                rbtn2ndRadiology2.Checked = false;
                rbtn2ndRadiology3.Checked = true;
                rbtn2ndRadiology4.Checked = false;
                rbtn2ndRadiology5.Checked = false;
            }
            else if (currentPatient.Param_2ndRadiology == 4)
            {
                rbtn2ndRadiology0.Checked = false;
                rbtn2ndRadiology1.Checked = false;
                rbtn2ndRadiology2.Checked = false;
                rbtn2ndRadiology3.Checked = false;
                rbtn2ndRadiology4.Checked = true;
                rbtn2ndRadiology5.Checked = false;
            }
            else if (currentPatient.Param_2ndRadiology == 5)
            {
                rbtn2ndRadiology0.Checked = false;
                rbtn2ndRadiology1.Checked = false;
                rbtn2ndRadiology2.Checked = false;
                rbtn2ndRadiology3.Checked = false;
                rbtn2ndRadiology4.Checked = false;
                rbtn2ndRadiology5.Checked = true;
            }

            // Param_3rdRadiology
            if (currentPatient.Param_3rdRadiology == 0)
            {
                rbtn3rdRadiology0.Checked = true;
                rbtn3rdRadiology1.Checked = false;
                rbtn3rdRadiology2.Checked = false;
                rbtn3rdRadiology3.Checked = false;
                rbtn3rdRadiology4.Checked = false;
                rbtn3rdRadiology5.Checked = false;
            }
            else if (currentPatient.Param_3rdRadiology == 1)
            {
                rbtn3rdRadiology0.Checked = false;
                rbtn3rdRadiology1.Checked = true;
                rbtn3rdRadiology2.Checked = false;
                rbtn3rdRadiology3.Checked = false;
                rbtn3rdRadiology4.Checked = false;
                rbtn3rdRadiology5.Checked = false;
            }
            else if (currentPatient.Param_3rdRadiology == 2)
            {
                rbtn3rdRadiology0.Checked = false;
                rbtn3rdRadiology1.Checked = false;
                rbtn3rdRadiology2.Checked = true;
                rbtn3rdRadiology3.Checked = false;
                rbtn3rdRadiology4.Checked = false;
                rbtn3rdRadiology5.Checked = false;
            }
            else if (currentPatient.Param_3rdRadiology == 3)
            {
                rbtn3rdRadiology0.Checked = false;
                rbtn3rdRadiology1.Checked = false;
                rbtn3rdRadiology2.Checked = false;
                rbtn3rdRadiology3.Checked = true;
                rbtn3rdRadiology4.Checked = false;
                rbtn3rdRadiology5.Checked = false;
            }
            else if (currentPatient.Param_3rdRadiology == 4)
            {
                rbtn3rdRadiology0.Checked = false;
                rbtn3rdRadiology1.Checked = false;
                rbtn3rdRadiology2.Checked = false;
                rbtn3rdRadiology3.Checked = false;
                rbtn3rdRadiology4.Checked = true;
                rbtn3rdRadiology5.Checked = false;
            }
            else if (currentPatient.Param_3rdRadiology == 5)
            {
                rbtn3rdRadiology0.Checked = false;
                rbtn3rdRadiology1.Checked = false;
                rbtn3rdRadiology2.Checked = false;
                rbtn3rdRadiology3.Checked = false;
                rbtn3rdRadiology4.Checked = false;
                rbtn3rdRadiology5.Checked = true;
            }

            // Param_4thRadiology
            if (currentPatient.Param_4thRadiology == 0)
            {
                rbtn4thRadiology0.Checked = true;
                rbtn4thRadiology1.Checked = false;
                rbtn4thRadiology2.Checked = false;
                rbtn4thRadiology3.Checked = false;
                rbtn4thRadiology4.Checked = false;
                rbtn4thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_4thRadiology == 1)
            {
                rbtn4thRadiology0.Checked = false;
                rbtn4thRadiology1.Checked = true;
                rbtn4thRadiology2.Checked = false;
                rbtn4thRadiology3.Checked = false;
                rbtn4thRadiology4.Checked = false;
                rbtn4thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_4thRadiology == 2)
            {
                rbtn4thRadiology0.Checked = false;
                rbtn4thRadiology1.Checked = false;
                rbtn4thRadiology2.Checked = true;
                rbtn4thRadiology3.Checked = false;
                rbtn4thRadiology4.Checked = false;
                rbtn4thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_4thRadiology == 3)
            {
                rbtn4thRadiology0.Checked = false;
                rbtn4thRadiology1.Checked = false;
                rbtn4thRadiology2.Checked = false;
                rbtn4thRadiology3.Checked = true;
                rbtn4thRadiology4.Checked = false;
                rbtn4thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_4thRadiology == 4)
            {
                rbtn4thRadiology0.Checked = false;
                rbtn4thRadiology1.Checked = false;
                rbtn4thRadiology2.Checked = false;
                rbtn4thRadiology3.Checked = false;
                rbtn4thRadiology4.Checked = true;
                rbtn4thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_4thRadiology == 5)
            {
                rbtn4thRadiology0.Checked = false;
                rbtn4thRadiology1.Checked = false;
                rbtn4thRadiology2.Checked = false;
                rbtn4thRadiology3.Checked = false;
                rbtn4thRadiology4.Checked = false;
                rbtn4thRadiology5.Checked = true;
            }

            // Param_5thRadiology
            if (currentPatient.Param_5thRadiology == 0)
            {
                rbtn5thRadiology0.Checked = true;
                rbtn5thRadiology1.Checked = false;
                rbtn5thRadiology2.Checked = false;
                rbtn5thRadiology3.Checked = false;
                rbtn5thRadiology4.Checked = false;
                rbtn5thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_5thRadiology == 1)
            {
                rbtn5thRadiology0.Checked = false;
                rbtn5thRadiology1.Checked = true;
                rbtn5thRadiology2.Checked = false;
                rbtn5thRadiology3.Checked = false;
                rbtn5thRadiology4.Checked = false;
                rbtn5thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_5thRadiology == 2)
            {
                rbtn5thRadiology0.Checked = false;
                rbtn5thRadiology1.Checked = false;
                rbtn5thRadiology2.Checked = true;
                rbtn5thRadiology3.Checked = false;
                rbtn5thRadiology4.Checked = false;
                rbtn5thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_5thRadiology == 3)
            {
                rbtn5thRadiology0.Checked = false;
                rbtn5thRadiology1.Checked = false;
                rbtn5thRadiology2.Checked = false;
                rbtn5thRadiology3.Checked = true;
                rbtn5thRadiology4.Checked = false;
                rbtn5thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_5thRadiology == 4)
            {
                rbtn5thRadiology0.Checked = false;
                rbtn5thRadiology1.Checked = false;
                rbtn5thRadiology2.Checked = false;
                rbtn5thRadiology3.Checked = false;
                rbtn5thRadiology4.Checked = true;
                rbtn5thRadiology5.Checked = false;
            }
            else if (currentPatient.Param_5thRadiology == 5)
            {
                rbtn5thRadiology0.Checked = false;
                rbtn5thRadiology1.Checked = false;
                rbtn5thRadiology2.Checked = false;
                rbtn5thRadiology3.Checked = false;
                rbtn5thRadiology4.Checked = false;
                rbtn5thRadiology5.Checked = true;
            }

            // Param_RadiologyAnswer
            if (currentPatient.Param_RadiologyAnswer == 0)
            {
                rbtnRadiologyAnswer0.Checked = true;
                rbtnRadiologyAnswer1.Checked = false;
                rbtnRadiologyAnswer2.Checked = false;
                rbtnRadiologyAnswer3.Checked = false;
                rbtnRadiologyAnswer4.Checked = false;
                rbtnRadiologyAnswer5.Checked = false;
                rbtnRadiologyAnswer6.Checked = false;
            }
            else if (currentPatient.Param_RadiologyAnswer == 1)
            {
                rbtnRadiologyAnswer0.Checked = false;
                rbtnRadiologyAnswer1.Checked = true;
                rbtnRadiologyAnswer2.Checked = false;
                rbtnRadiologyAnswer3.Checked = false;
                rbtnRadiologyAnswer4.Checked = false;
                rbtnRadiologyAnswer5.Checked = false;
                rbtnRadiologyAnswer6.Checked = false;
            }
            else if (currentPatient.Param_RadiologyAnswer == 2)
            {
                rbtnRadiologyAnswer0.Checked = false;
                rbtnRadiologyAnswer1.Checked = false;
                rbtnRadiologyAnswer2.Checked = true;
                rbtnRadiologyAnswer3.Checked = false;
                rbtnRadiologyAnswer4.Checked = false;
                rbtnRadiologyAnswer5.Checked = false;
                rbtnRadiologyAnswer6.Checked = false;
            }
            else if (currentPatient.Param_RadiologyAnswer == 3)
            {
                rbtnRadiologyAnswer0.Checked = false;
                rbtnRadiologyAnswer1.Checked = false;
                rbtnRadiologyAnswer2.Checked = false;
                rbtnRadiologyAnswer3.Checked = true;
                rbtnRadiologyAnswer4.Checked = false;
                rbtnRadiologyAnswer5.Checked = false;
                rbtnRadiologyAnswer6.Checked = false;
            }
            else if (currentPatient.Param_RadiologyAnswer == 4)
            {
                rbtnRadiologyAnswer0.Checked = false;
                rbtnRadiologyAnswer1.Checked = false;
                rbtnRadiologyAnswer2.Checked = false;
                rbtnRadiologyAnswer3.Checked = false;
                rbtnRadiologyAnswer4.Checked = true;
                rbtnRadiologyAnswer5.Checked = false;
                rbtnRadiologyAnswer6.Checked = false;
            }
            else if (currentPatient.Param_RadiologyAnswer == 5)
            {
                rbtnRadiologyAnswer0.Checked = false;
                rbtnRadiologyAnswer1.Checked = false;
                rbtnRadiologyAnswer2.Checked = false;
                rbtnRadiologyAnswer3.Checked = false;
                rbtnRadiologyAnswer4.Checked = false;
                rbtnRadiologyAnswer5.Checked = true;
                rbtnRadiologyAnswer6.Checked = false;
            }
            else if (currentPatient.Param_RadiologyAnswer == 6)
            {
                rbtnRadiologyAnswer0.Checked = false;
                rbtnRadiologyAnswer1.Checked = false;
                rbtnRadiologyAnswer2.Checked = false;
                rbtnRadiologyAnswer3.Checked = false;
                rbtnRadiologyAnswer4.Checked = false;
                rbtnRadiologyAnswer5.Checked = false;
                rbtnRadiologyAnswer6.Checked = true;
            }
            
            if (currentPatient.Param_CirculatoryAffectedBowels)
            {
                CirculatoryAffectedNoRadioButton.Checked = false;
                CirculatoryAffectedYesRadioButton.Checked = true;
            }
            else
            {
                CirculatoryAffectedNoRadioButton.Checked = true;
                CirculatoryAffectedYesRadioButton.Checked = false;
            }
        }

        private void SetupControls(int totalPatients, int currentPatientIndex, PatientDTO currentPatient)
        {
            if (totalPatients == 1)
            {
                PrevLinkButton.Enabled = false;
                PrevLinkButton.CssClass = "btn btn-large disabled";

                NextLinkButton.Enabled = false;
                NextLinkButton.CssClass = "btn btn-large disabled";
            }
            else if (currentPatientIndex == 1)
            {
                PrevLinkButton.Enabled = false;
                PrevLinkButton.CssClass = "btn btn-large disabled";

                NextLinkButton.Enabled = true;
                NextLinkButton.CssClass = "btn btn-large";
            }
            else if (currentPatientIndex == totalPatients)
            {
                PrevLinkButton.Enabled = true;
                PrevLinkButton.CssClass = "btn btn-large";

                NextLinkButton.Enabled = false;
                NextLinkButton.CssClass = "btn btn-large disabled";
            }
            else
            {
                PrevLinkButton.Enabled = true;
                PrevLinkButton.CssClass = "btn btn-large";

                NextLinkButton.Enabled = true;
                NextLinkButton.CssClass = "btn btn-large";
            }

            if (currentPatient.IsFlagged)
            {
                FlagLinkButton.CssClass = "btn btn-large active";
                FlagLinkButton.ToolTip = "Mark as Unflag";
            }
            else
            {
                FlagLinkButton.CssClass = "btn btn-large";
                FlagLinkButton.ToolTip = "Mark as Flag";
            }

            if (currentPatient.IsReviewed)
            {
                ReviewLinkButton.CssClass = "btn btn-large active";
                ReviewLinkButton.ToolTip = "Unmark as Reviewed";
            }
            else
            {
                ReviewLinkButton.CssClass = "btn btn-large";
                ReviewLinkButton.ToolTip = "Mark as Reviewed";
            }
        }

        protected void RedirectUser(UserDTO userDTO)
        {
            if (userDTO != null)
            {
                if (userDTO.UserType == "Administrator")
                {
                    Response.Redirect("AdministratorDashboard.aspx");
                }
            }
            else
            {
                Response.Redirect("Index.aspx");
            }
        }
    }
}

